﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TTransferBinOrderDetailRepository : BaseRepository<TTransferBinOrderDetail>, ITTransferBinOrderDetailRepository
    {
        public TTransferBinOrderDetailRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 添加移库单明细
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddTransferBinOrderDetails(TTransferBinOrderDetail obj)
        {
            var trans = Context.TTransferBinOrders.FirstOrDefault(r=>r.Id==obj.OrderId);
            if (trans == null)
            {
                return false;
            }
            obj.CreateUserId = trans.CreateUserId;
            obj.CreateUserName = obj.CreateUserName;
            obj.CreateDate = DateTime.Now;
            
            Context.TTransferBinOrderDetails.Add(obj);
            return Context.SaveChanges() > 0;
        }

        /// <summary>
        /// 根据id删除移库单明细
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public bool DelTransferBinOrderDetails(int tid)
        {
            var obj = Context.TTransferBinOrderDetails.Find(tid);
            if (obj != null)
            {
                Context.TTransferBinOrderDetails.Remove(obj);
            }
            return Context.SaveChanges() > 0;
        }
        /// <summary>
        /// 查询移库单明细信息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TTransferBinOrderDetail> GetTransferBinOrderDetailsList(int orderId, int pageIndex, int pageSize, out int totalCount)
        {
            var list = Context.TTransferBinOrderDetails.AsNoTracking().ToList();
            totalCount = list.Count();
            if (orderId == null)
            {
                return list;
            }
            return list.Where(t => t.OrderId == orderId).ToList();
        }

        /// <summary>
        /// 根据id修改移库单明细
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool UpdateTransferBinOrderDetails(TTransferBinOrderDetail t)
        {
            var trans = Context.TTransferBinOrders.FirstOrDefault(r => r.Id == t.OrderId);
            if (trans == null)
            {
                return false;
            }
            t.ModifyUserId = trans.ModifyUserId;
            t.ModifyUserName = trans.ModifyUserName;
            t.ModifyDate = DateTime.Now;

            Context.TTransferBinOrderDetails.Update(t);
            return Context.SaveChanges() > 0;
        }
    }
}
